<template>
	<view>
		<view class="bluetooth-status">
			<image src="/static/lanya.png" class="icon"></image>
			<view class="desc" v-if="bluetoothStatus">蓝牙已开启，连接成功！</view>
			<view class="desc" v-else @click="bluetoothReset">蓝牙未连接，<text
					style="color: blue;text-decoration: underline;">点击连接</text></view>
			<view class="num">机器：{{equipmentNo}}</view>
		</view>
		<view class="place"></view>
		<view class="">
			这是一个测试页面
		</view>
		<input type="text" v-model="tid" placeholder="请输入8位数字">
		<button @click="setBluetooth">更改蓝牙名称</button>
	</view>
</template>

<script>
	import md5 from '@/utils/md5.js'
	import bluetoothDevice from '@/utils/bluetooth.js'
	export default {
		data() {
			return {
				tid: '',
				num: '',
				skey: '',
				md5: '',
				account:'',
				equipmentNo:''			}
		},
		computed: {
			bluetoothStatus() {
				return this.$store.getters.bluetoothStatus
			},
			bluetoothObj() {
				return this.$store.getters.bluetoothObj
			}
		},
		onLoad(option) {
			if(option.equipmentNo.substring(0,2)=='MD'){
				this.bluetoothReset()
				this.equipmentNo=option.equipmentNo
				this.account = option.equipmentNo.substring(2,4)
				
			if(!/^\d$/.test(this.account)){
				this.account ='0'+this.account.substring(0,1)
			}
			this.skey=this.randomWord(1,16)
			for (let i = 0; i < 2; i++) {
				this.num += Math.round(Math.random() * 10);
			}
			// for (let i = 0; i < 8; i++) {
			// 	this.tid += Math.round(Math.random() * 10);
			// }
			console.log(this.account,this.skey,this.num)
			}
			
		},
		methods: {
			bluetoothReset(){
				const bluetoothObj = new bluetoothDevice()
				this.$store.commit('user/SET_BLUETOOTH_OBJ', bluetoothObj);
				this.bluetoothObj.initEventDevice()
			},
			 randomWord: function(randomFlag, min) {
			      let str = ""
				  let range = min
			      let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
			      for (let i = 0; i < range; i++) {
			        var pos = Math.round(Math.random() * (arr.length - 1));
			        str += arr[pos];
			      }
			      return str;
			},
			setBluetooth(){
				if(/^\d{8}$/.test(this.tid)){
					// this.md5=md5.hex_md5(this.tid+this.num+this.skey).substring(0,5)
					this.md5=md5.hex_md5('00000000'+this.num+'0000000000000000').substring(0,5)
					// this.bluetoothObj.writeDevice('<Z4590089|00000890|3Zbro1K30IUT8TpB|03>')
					// this.bluetoothObj.writeDevice('<C'+this.md5+this.num+'|1|1>')
					this.bluetoothObj.writeDevice('<Z'+this.md5+this.num+'|'+this.tid+'|'+this.skey+'|'+this.account+'>')
				}else{
					uni.showToast({
					    duration: 2500,
					    title: "请输入8位数字",
					    icon: 'error',
					})
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.bluetooth-status {
		position: fixed;
		left: 0;
		right: 0;
		display: flex;
		z-index: 2;
		align-items: center;
		padding: 0 43rpx 0 54rpx;
		background: #ED602B;

		.icon {
			width: 15rpx;
			height: 24rpx;
		}

		.desc {
			flex: 1;
			margin-left: 15rpx;
			line-height: 61rpx;
			font-size: 26rpx;
			color: #fff;
		}

		.num {
			line-height: 61rpx;
			font-size: 26rpx;
			color: #fff;
		}
	}
	.place{
		width: 1rpx;
		height: 1rpx;
		margin-bottom: 20rpx;
	}
</style>
